<html>
<head><meta charset="utf-8"><title>const str concatenation · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20str.20concatenation.html">const str concatenation</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="205074803"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20str%20concatenation/near/205074803" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20str.20concatenation.html#205074803">(Jul 27 2020 at 01:04)</a>:</h4>
<p>Is it possible, using current const evaluation, to do the equivalent of <code>concat!</code> with values that are all either literals or constants of type <code>&amp;str</code>?</p>



<a name="205074919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20str%20concatenation/near/205074919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20str.20concatenation.html#205074919">(Jul 27 2020 at 01:09)</a>:</h4>
<p>(I don't need full string formatting, just concatenation.)</p>



<a name="205076689"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20str%20concatenation/near/205076689" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20str.20concatenation.html#205076689">(Jul 27 2020 at 02:07)</a>:</h4>
<p>I think that to do it in a general const context (non-macro) would take const allocation, and i'm pretty sure it'd be all over the news if that was supported, even just in nightly.</p>



<a name="205082651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20str%20concatenation/near/205082651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20str.20concatenation.html#205082651">(Jul 27 2020 at 05:17)</a>:</h4>
<p>well, you can cheat with a large enough array, but that limits the maximum length of your concat invocation. You can make that length configurable though. The main blocker for even that is unsafe things</p>



<a name="205148902"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20str%20concatenation/near/205148902" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20str.20concatenation.html#205148902">(Jul 27 2020 at 18:03)</a>:</h4>
<p>So, the fact that they're all <code>&amp;'static str</code> values doesn't help in any way?</p>



<a name="205150069"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20str%20concatenation/near/205150069" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20str.20concatenation.html#205150069">(Jul 27 2020 at 18:14)</a>:</h4>
<p>If you are just interested in getting it to work for now, using macros is a solution: <a href="https://stackoverflow.com/a/35159310/155423">How to concatenate static strings in Rust</a></p>



<a name="205435481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20str%20concatenation/near/205435481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matt1992 <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20str.20concatenation.html#205435481">(Jul 30 2020 at 02:20)</a>:</h4>
<p>I was able to make a macro to concatenate constant <code>&amp;'static str</code> (that don't come from generic parameters):<br>
<a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=1555757f31d8b98f514806c4f94b634c">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=1555757f31d8b98f514806c4f94b634c</a><br>
It relies on the layout of <code>&amp;[u8]</code> and <code>&amp;str</code> (the pointer itself) being the same.</p>



<a name="205449824"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20str%20concatenation/near/205449824" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20str.20concatenation.html#205449824">(Jul 30 2020 at 07:43)</a>:</h4>
<p>whoa whoa, nice!</p>



<a name="205459768"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20str%20concatenation/near/205459768" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20str.20concatenation.html#205459768">(Jul 30 2020 at 09:51)</a>:</h4>
<p>this old one seemed nice to use as well <a href="https://github.com/Vurich/const-concat/blob/master/src/lib.rs">https://github.com/Vurich/const-concat/blob/master/src/lib.rs</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>